package com.axzhengxin.platform.common.util;

import com.ruoyi.common.utils.DateUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;

/**
 * SearchQueryBuilder
 *
 * @author jiangnan
 * @date 2021/3/18
 */
public class SearchQueryBuilder {


    // 获取一个RangeQuery查询
    public static BoolQueryBuilder setRangeQueryLevel(BoolQueryBuilder boolQueryBuilder, String fieldName, String level) {

        if ("REGCAP".equals(fieldName)) {
            switch (level) {
                case "1":
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).gte(0).lte(100));
                    break;
                case "2":
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).gte(100).lte(500));
                    break;
                case "3":
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).gte(500).lte(1000));
                    break;
                case "4":
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).gte(1000));
                    break;
                default:
                    break;
            }

        } else if ("ESTDATE".equals(fieldName)) {
            switch (level) {

                case "1"://成立6个月以内
                    System.out.println("CommonUtils.getAfterNDay(-7)==" + DateUtils.getAfterNDay(-7));
                    System.out.println("CommonUtils.getCurDate1()==" + DateUtils.getTime());
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd HH:mm:ss").gte(DateUtils.getAfterNMonthTime(-6)).lte(DateUtils.getTime()));
                    break;
                case "2"://成立6个月到两年内的企业
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd HH:mm:ss").gte(DateUtils.getAfterNYearTime(-2)).lte(DateUtils.getAfterNMonthTime(-6)));
                    break;
                case "3"://成立2-5年以内
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd HH:mm:ss").gte(DateUtils.getAfterNYearTime(-5)).lte(DateUtils.getAfterNYearTime(-2)));
                    break;
                case "4"://成立5-10年以内
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd HH:mm:ss").gte(DateUtils.getAfterNYearTime(-10)).lte(DateUtils.getAfterNYearTime(-5)));
                    break;
                case "5"://成立10年周以上
                    boolQueryBuilder.filter(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd HH:mm:ss").lte(DateUtils.getAfterNYearTime(-10)));
                    break;
                default:
                    break;
            }
        }
        return boolQueryBuilder;
    }

    // 获取一个RangeQuery查询
    public static RangeQueryBuilder getRangeQueryInteger(String fieldName, Integer gte, Integer lte) {

        return QueryBuilders.rangeQuery(fieldName).gte(gte).lte(lte);
    }

    // 获取一个RangeQuery查询
    public static RangeQueryBuilder getRangeQueryDate(String fieldName, String gte, String lte) {

        return QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd HH:mm:ss").gte(gte).lte(lte);

    }

    // 获取一个TermQuery查询
    public static BoolQueryBuilder setTermQuery(BoolQueryBuilder boolQueryBuilder, String fieldName, String termString) {

        if (termString != null && !("").equals(termString)) {
            boolQueryBuilder.filter(QueryBuilders.termQuery(fieldName, termString));
        }
        return boolQueryBuilder;
    }

    // 获取一个TermsQuery查询
    public static BoolQueryBuilder setTermsQuery(BoolQueryBuilder boolQueryBuilder, String fieldName, String[] termsString) {
        if (termsString != null && termsString.length > 0) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery(fieldName, termsString));
        }
        return boolQueryBuilder;
    }

    // 获取一个TermsQuery查询
    public static BoolQueryBuilder setTermsQuery(BoolQueryBuilder boolQueryBuilder, String fieldName, String termsString) {
        if (termsString != null && !("").equals(termsString)) {
            String[] terms = termsString.split(",");
            if (terms.length > 0) {
//					for (int i = 0; i < terms.length; i++) {
//						if (terms[i].equals("")) {
//
//						}
//					}
                boolQueryBuilder.filter(QueryBuilders.termsQuery(fieldName, terms));
            }

        }

        return boolQueryBuilder;
    }


    // 获取一个ExiteQuery查询
    public static BoolQueryBuilder setExistsQueryType(BoolQueryBuilder boolQueryBuilder, String termString) {
        switch (termString) {
            case "hzs":
                boolQueryBuilder.filter(QueryBuilders.existsQuery("HZS_NAME"));
                break;
            case "jtnc":
                boolQueryBuilder.filter(QueryBuilders.existsQuery("JTNC_NAME"));
                break;
            case "lyqy":
                boolQueryBuilder.filter(QueryBuilders.existsQuery("LTQY_NAME"));
                break;
            default:
                break;
        }
        return boolQueryBuilder;

    }

    // 获取一个ExiteQuery查询
    @SuppressWarnings("deprecation")
    public static BoolQueryBuilder setExisteOrMissFilter(BoolQueryBuilder boolQueryBuilder, String fieldName, String level) {

        switch (level) {
            case "1":
                boolQueryBuilder.filter(QueryBuilders.existsQuery(fieldName));
                break;
            case "2":
                boolQueryBuilder.filter(QueryBuilders.missingQuery(fieldName));
                break;

            default:
                break;
        }
        return boolQueryBuilder;

    }

}
